package com.fzshang.shopping.api.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.fzshang.shopping.commen.bean.Classification;
import com.fzshang.shopping.commen.bean.CommodityClassification;
import com.fzshang.shopping.commen.bean.layuiBean.ResultPage;
import com.fzshang.shopping.commen.bean.layuiBean.VoPage;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-07
 */
@Controller
@RequestMapping("admin/classification")
public class ClassificationController extends BaseController {

    @RequestMapping(value = "classification.html")
    public String classification(Model model){
        model.addAttribute("commodity",commodityClassificationService.list());
        return "admin/classification/classification";
    }
    @RequestMapping(value = "classification_add.html")
    public String classification_add(Model model){
        model.addAttribute("commodityClassification",commodityClassificationService.list());
        return "admin/classification/classification_add";
    }
    @RequestMapping(value = "classification_edit.html")
    public String classification_edit(Model model,Long id){
        model.addAttribute("commodityClassification",commodityClassificationService.list());
        Classification classification = classificationService.getById(id);
        model.addAttribute("classification",classification);
        return "admin/classification/classification_edit";
    }


    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Classification> selectMyPage(VoPage voPage, Classification classification){
        return classificationService.selectMyPage(voPage,classification);
    }
    /**
     * @param classification
     * @return 添加和修改
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public ApiResult save( Classification classification,String prods){
        //去除空的  防止json转换失败
        prods= StrUtil.removeAll(prods,"[],");
        prods= StrUtil.removeAll(prods,",[]");
        List<Classification> classifications = JSONObject.parseArray(prods,Classification.class);
        for (int i = 0; i < classifications.size(); i++) {
            classifications.get(i).setCreateTime(System.currentTimeMillis());
            classifications.get(i).setClassificationId(classification.getClassificationId());
            classifications.get(i).setType(classification.getType());
        }
        if(classificationService.saveBatch(classifications)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }
    /**
     * @param id
     * @return 删除
     */
    @RequestMapping(value = "deleteById")
    @ResponseBody
    public ApiResult deleteById(Long id){
        if(classificationService.removeById(id)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }
    /**
     * @param
     * @return 修改
     */
    @RequestMapping(value = "updateById")
    @ResponseBody
    public ApiResult updateById(@RequestBody Classification classification){
        if(classificationService.updateById(classification)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param id
     * @return 获取分类id
     */
    @RequestMapping(value = "getClassificationId")
    @ResponseBody
    public ApiResult getClassificationId(Long id){
            return ApiResult.ok(classificationService.getById(id));

    }


}
